﻿using System.Threading.Tasks;
using Grpc.Net.Client;

namespace ParkManagement
{
    public class RpcUserServiceRealize
    {
        public Task<UserDto> GetAsync(int id)
        {
            using (var grpcChannel = GrpcChannel.ForAddress("http://localhost:10002"))
            {
                var grpcClient = new RpcUserService.RpcUserServiceClient(grpcChannel);
                var grpcResponse = grpcClient.GetAsync(new RpcUserRequest()
                {
                    Id = id.ToString()
                });
                var rpcUser = grpcResponse.User;
                var userDto = new UserDto()
                {
                    UserId = int.Parse(rpcUser.UserId),
                    UserName = rpcUser.UserName,
                    Avatar = rpcUser.Avatar,
                    NickName = rpcUser.NickName,
                    Sex = rpcUser.Sex,
                    Phonenumber = rpcUser.Phonenumber,
                    Email = rpcUser.Email,
                    IdCard = rpcUser.IdCard,
                    Balance = decimal.Parse(rpcUser.Balance),
                    Score = int.Parse(rpcUser.Score)
                };
                return Task.FromResult(userDto);
            }
        }
    }
}